Gatsby Default StarterGatsby logo

#Algorithmic

The aim is to create a simplified tic-tac-toe.

  1. Two players take turns entering a coordinate (row, column).
  2. The first player is asked to enter a coordinate, and we check their validity; i.e., we want to make sure that the coordinate is in the board, and that it hasn't already been played.
  3. We add "X" to the board at the right coordinate if it's valid.
  4. Then move on to player 2, ask for a new coordinate and, if valid, add "O".
  5. Repeat until one of the two players has won, or the board has been filled.
  6. At the end of the game, you indicate whether player 1 has won, player 2 has won, or neither has won.
  7. You are then asked if you want to play again.

I'll help you get started (and break down the problem). You don't have to follow this help if you think you've got an equivalent or better solution!

1. To initialize our playground, we declare an array of 3 arrays containing 3 "*". Example : area = [["*" for x in range(3)] for y in range(3)]

2. We prioritize and save the complex part for last:

  • First, we want to be able to ask a user for a coordinate and check that it's valid.
  • We want to be able to add a valid coordinate to the array ("O")
  • After a move, we want to be able to display the playing area/our board (area). Here, we're already able to ask a player to make a valid move and display it, which is pretty cool.
  • We want to be able to play (ask for a coordinate) as long as the board isn't full.
  • We want to be able to add "O" or "X" alternately, depending on which player is playing.

Once we have these elements, we'll be well on our way. We'll be able to fill an array with "O" or "X" until it's full (i.e. once it's full, the program stops).

  • We now want to be able to ask the user if he wants to play again, and restart the game if he does.

3. We attack the complicated part:

  • After each move, we want to be able to check whether the game is over. To do this, after each move we'll have to check whether we have a winning combination horizontally, vertically or diagonally.
  • We want to be able to tell whether player 1 has won, player 2 has won, or no player has won, once the game is over.

And that's all you need for a complete, simplified tic-tac-toe game! All that's left is to get everything to work together.

Obviously, we'll be looking to develop our script by creating functions to make our code cleaner.

![[Pasted image 20240515113837.png]]

As a rough guide:

I have in global variables:

  • area: a variable representing my game board
  • player: a variable alternating between 1 and 2 to know which player is playing
  • playedLine: a variable corresponding to the last line played at a given time.
  • playedColumn: a variable corresponding to the last column played at time T

In Python, to use a global variable inside a function/procedure, use the keyword; global.

And in functions/procedures:

  • initarea(): a procedure that generates area with only "*"
  • readcoord(): a procedure that will request 2 coordinates (row, column) from a user
  • checkcoord(line, column): a function which will check the validity of the entered coordinate
  • getplayersymbol(): a procedure that will return "X" or "O" depending on the player playing
  • applycoord(line, column): a procedure that will add "X" or "O" to the valid coordinate
  • checkhoriz(line, column): a function that tests if the line on which the parameter coordinate is located is victorious
  • checkvert(line, column): a function that tests if the column on which the parameter coordinate is located is victorious
  • checkdiag(line, column): a function which tests whether the diagonal(s) associated with the parameter coordinate is/are victorious
  • checkifgameiswon(): a function that tests if the game is won by one of the two players
  • checkifareaisfull(): a function that tests if all squares in area have been played
  • replay(): a function that asks if we want to replay
  • play(): a procedure that manages the actions to be performed during each game turn
  • changeplayer(): a procedure that takes care of changing the player who is playing
  • displayarea(): a procedure that displays area every round
  • launchgame(): a procedure that takes care of connecting my functions/procedures

![[Pasted image 20240515115859.png]]